package com.ttmall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {

    public  static  ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1：thread id : " + Thread.currentThread().getId());
            return 1;
        }, service);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务3：thread id : " + Thread.currentThread().getId());
            return 2;
        }, service);

        CompletableFuture<Integer> integerCompletableFuture = future1.thenCombineAsync(future2, (e1, e2) -> e1 + e2, service);
        Integer integer = integerCompletableFuture.get();
        System.out.println(integer);


        //future1.thenAcceptBothAsync(future2 , (e1,e2)-> {
        //    System.out.println(e1);
        //    System.out.println(e2);
        //    System.out.println("任务三");
        //}, service);

        //future1.runAfterBothAsync(future2 , ()-> System.out.println("任务三"), service);


        //CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        //    System.out.println("thread id : " + Thread.currentThread().getId());
        //
        //}, service);

        //CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
        //    System.out.println("thread id : " + Thread.currentThread().getId());
        //    return 10 % 2;
        //}, service).handle((res,throwable) ->{
        //    if (res !=  null){
        //        return res * 2;
        //    }
        //    if (throwable != null){
        //        return 0;
        //    }
        //    return 0;
        //})
        //        .whenComplete((res, throwable)->{
        //    System.out.println("返回结果：" + res);
        //    System.out.println("异常：" + throwable);
        //}).exceptionally(throwable -> {
        //    return 10;
        //})
                ;
        //CompletableFuture.supplyAsync(() -> {
        //    System.out.println("thread id : " + Thread.currentThread().getId());
        //    return 10 / 2;
        //}, service).thenApplyAsync((res)->{
        //
        //    return "..." + res;
        //} , service);

        //        .thenAccept((e)->{
        //    System.out.println("任务2----------"+ e);
        //});

        //        .thenRun(() ->{
        //    System.out.println("任务2");
        //}) ;

        //Integer integer = future1.get();
        //System.out.println(integer);

        //System.out.println("main  thread id : " + Thread.currentThread().getId());

        //Thread thread = new ThreadTest.Test01();
        //thread.start();;
        //Runnable01 runnable01 = new Runnable01();
        //new Thread(runnable01).start();

        //FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable1());
        //
        //new Thread(integerFutureTask).start();
        //
        //Integer integer = integerFutureTask.get();
        //
        //System.out.println(integer);

        //ExecutorService executorService = Executors.newFixedThreadPool(10);
        //
        //executorService.submit(()-> System.out.println("aa"));

        //ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
        //        200,
        //        10 ,
        //        TimeUnit.SECONDS,
        //        new LinkedBlockingDeque<>(1000),
        //        Executors.defaultThreadFactory(),
        //        new ThreadPoolExecutor.AbortPolicy());

    }

    public  static  class  Test01 extends  Thread{
        @Override
        public void run() {
            System.out.println("thread id : " + Thread.currentThread().getId());
        }
    }

    public  static  class  Runnable01 implements Runnable{
        @Override
        public void run() {
            System.out.println("Runnable01 thread id : " + Thread.currentThread().getId());
        }
    }

    public  static  class Callable1 implements java.util.concurrent.Callable<Integer>{
        @Override
        public Integer call() throws Exception {
            System.out.println("Callable thread id : " + Thread.currentThread().getId());
            return 1;
        }
    }

}
